<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/2/002
 * Time: 9:16
 */

namespace app\Lib\Model\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\DbModel\OrderBindingTag;
use app\Lib\Model\Tag\OrderStopWordReason\StopWordTag;
use app\Lib\Model\User\User;

class HangStopWordTag
{
    private $opUser;
    private $stopWordTag;
    private $order;
    private $content;
    public function __construct(User $opUser ,StopWordTag $stopWordTag ,Order $order)
    {
        $this->opUser = $opUser;
        $this->stopWordTag = $stopWordTag;
        $this->order = $order;
    }

    public function setContent($content){
        $this->content = $content;
    }

    /**
     * 执行
     * @return int|mixed|string
     * @throws BlException
     */
    public function run(){
        $result =  $this->order->is_hand_stop_word();
        if($result){
            throw new BlException('该工单已停工，无法再次停工');
        }
        $model =  new OrderBindingTag();
        $data = [
            $model->_order_id=>$this->order->get_order_id(),
            $model->_create_time=>time(),
            $model->_create_uid=>$this->opUser->getUid(),
            $model->_reason=>$this->content,
            $model->_tag_id=>$this->stopWordTag->getTagId(),
        ];
        $result =  $model->insert($data);
        if(!$result){
            throw new BlException('选择停工标签失败');
        }
        ((new OrderPush($this->order)))->stopWordPush();
        return $result;
    }

}